
package com.smart.interceptor;

import cn.dev33.satoken.stp.StpUtil;
import com.smart.entity.SysUser;
import com.smart.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.core.context.MagicUser;
import org.ssssssss.magicapi.core.exception.MagicLoginException;
import org.ssssssss.magicapi.core.interceptor.AuthorizationInterceptor;

@Component
public class CustomAuthorizationInterceptor implements AuthorizationInterceptor {

    @Autowired
    private SysUserService sysUserService;
    @Override
    public boolean requireLogin() {
        return true;
    }

    @Override
    public MagicUser getUserByToken(String token) throws MagicLoginException {

            if(null!=StpUtil.getLoginIdByToken(token)){
               SysUser sysUser = sysUserService.getById(StpUtil.getLoginIdAsString());
                return new MagicUser(sysUser.getId()+"", sysUser.getNickname(), token);
            }
        throw new MagicLoginException("token无效");
    }

    @Override
    public MagicUser login(String username, String password) throws MagicLoginException {
        return AuthorizationInterceptor.super.login(username, password);
    }
}
